//
//  CustomStyleViewController.m
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/6.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import "CustomStyleViewController.h"
#import "ABButton.h"
#import <Masonry/Masonry.h>
#import <ReactiveObjC/ReactiveObjC.h>
#import "CustomContainerView.h"
#import "Customization.h"

@interface CustomStyleViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property(nonatomic, strong) UIPickerView *pickerView;
@property(nonatomic, strong) NSMutableArray<CustomizationStyleItem *> *maleStyles;
@property(nonatomic, strong) NSMutableArray<CustomizationStyleItem *> *femaleStyles;
@property(nonatomic, strong) ABButton *genderButton;

@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UILabel *selectedLabel;
@property(nonatomic, strong) UIImageView *scissorImageView;
@end

@implementation CustomStyleViewController

- (CustomizationStyle *)style {
    return self.customization.style;
}

- (NSMutableArray<CustomizationStyleItem *> *)maleStyles {
    if (!_maleStyles) {
        _maleStyles = [NSMutableArray array];
        for (NSInteger i= 0; i < 30; i++) {
            CustomizationStyleItem *style = [CustomizationStyleItem new];
            style.title = [NSString stringWithFormat:@"男士%@",@(i)];
            [_maleStyles addObject:style];
        }
    }
    return _maleStyles;
}

- (NSMutableArray<CustomizationStyleItem *> *)femaleStyles {
    if (!_femaleStyles) {
        _femaleStyles = [NSMutableArray array];
        for (NSInteger i= 0; i < 5; i++) {
            CustomizationStyleItem *style = [CustomizationStyleItem new];
            style.title = [NSString stringWithFormat:@"女士%@",@(i)];
            [_femaleStyles addObject:style];
        }
    }
    return _femaleStyles;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.canScrollToNext = NO;
    self.target = CustomSubviewControllerTypeDesign;

    UIImage *image = [UIImage imageNamed:@"custom_bg_style"];
    self.rightContainerView.backgroundImage = image;

    self.genderButton = [ABButton buttonWithStyle:ABButtonStyleFingerprint];
    [_genderButton setTitle:@"男装" forState:UIControlStateNormal];
    [_genderButton setTitle:@"女装" forState:UIControlStateSelected];
    [_genderButton addTarget:self action:@selector(genderButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.rightContainerView.rightButton = _genderButton;

    self.titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont systemFontOfSize:16];
    _titleLabel.text = @"品类选择";
    _titleLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    [self.rightContainerView.container addSubview:_titleLabel];

    //选中的
    self.selectedLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont systemFontOfSize:16];
    _selectedLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    [self.rightContainerView.container addSubview:_selectedLabel];

    //剪刀
    self.scissorImageView = [[UIImageView alloc] init];
    UIImage *scissorImage = [UIImage imageNamed:@"custom_scissor"];
    _scissorImageView.image = [scissorImage stretchImageWithInsetsTop:0 left:80 bottom:0 right:10];
    [self.rightContainerView.container addSubview:_scissorImageView];

    self.pickerView = [[UIPickerView alloc] init];
    _pickerView.layer.cornerRadius = 8;
    _pickerView.backgroundColor = [UIColor colorWithHexString:@"#f4f4f4"];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [self.rightContainerView.container addSubview:_pickerView];

    [self.nextButton setTitle:@"进入设计" forState:UIControlStateNormal];

    CustomizationStyle *style = self.style;
    //当前选中的
    RAC(_selectedLabel, text) = RACObserve(style, selectedItem.title);
    //性别切换
    @weakify(self)
    [RACObserve(style, gender) subscribeNext:^(id x) {
        @strongify(self)
        [self.pickerView reloadAllComponents];
        NSInteger index = [self.pickerView selectedRowInComponent:0];
        if (style.selectedItem != self.currentStyles[index]) {
            style.selectedItem = self.currentStyles[index];
        }
    }];

    //选择的模型种类改变
    [RACObserve(self.customization, selectedModel) subscribeNext:^(CustomizationModel  *model) {
        @strongify(self)
        [self.pickerView reloadAllComponents];
        NSInteger index = [self.pickerView selectedRowInComponent:0];
        if (style.selectedItem != self.currentStyles[index]) {
            style.selectedItem = self.currentStyles[index];
        }
    }];

    if (style.selectedItem) {
        NSInteger index = [self.currentStyles indexOfObject:style.selectedItem];
        if (index != NSNotFound) {
            [self.pickerView selectRow:index inComponent:0 animated:YES];
        }
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    UIView *container = self.rightContainerView.container;

    layoutViews(^{ //横屏
        [_scissorImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_titleLabel.mas_right).offset(3);
            make.top.equalTo(container).offset(71);
            make.height.equalTo(@32);
            make.right.equalTo(_selectedLabel.mas_left).offset(-3);
        }];

        [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(container).offset(32);
            make.width.equalTo(@68);
            make.centerY.equalTo(_scissorImageView);
        }];

        [_selectedLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(container).offset(-30);
            make.width.equalTo(@86);
            make.centerY.equalTo(_scissorImageView);
        }];

    }, ^{ //竖屏

        [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(container).offset(30);
            make.height.equalTo(@22);
            make.top.equalTo(container).offset(75);
        }];

        [_scissorImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_titleLabel.mas_right).offset(15);
            make.bottom.equalTo(container).offset(35);
            make.top.equalTo(container.mas_bottom).offset(2);
        }];

        [_selectedLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@22);
            make.left.equalTo(container).offset(110);
            make.bottom.equalTo(container).offset(-25);
        }];

    });

    [_pickerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        layoutViews(^{ //横屏
            make.left.equalTo(self.rightContainerView.container).offset(32);
            make.bottom.equalTo(self.rightContainerView.container).offset(-25);
            make.right.equalTo(self.rightContainerView.container).offset(-30);
            make.top.equalTo(_scissorImageView.mas_bottom).offset(16);
        }, ^{ //竖屏
            make.width.equalTo(self.rightContainerView.container).multipliedBy(0.75);
            make.bottom.equalTo(self.rightContainerView.container).offset(-26);
            make.right.equalTo(self.rightContainerView.container).offset(-30);
            make.top.equalTo(self.rightContainerView.container).offset(48);
        });
    }];
}

- (void)genderButtonClicked: (ABButton *)sender {
    sender.selected = !sender.isSelected;
    self.style.gender =  sender.isSelected ? GenderTypeFemale : GenderTypeMale; self.canScrollToNext = YES;
}


- (NSArray<CustomizationStyleItem *> *)currentStyles {
    return self.style.gender == GenderTypeMale ? self.maleStyles : self.femaleStyles;
}

#pragma mark - UIPickerView Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 48;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.currentStyles.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [NSString stringWithFormat:@"%@%@", self.currentStyles[row].title, self.customization.selectedModel.title];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.style.selectedItem = self.currentStyles[row];
}



@end
